package ModelLayer;
import java.util.ArrayList;

public class AddressBook
{
    private ArrayList<Person> _persons;
    private static AddressBook _instance;

    public AddressBook()
    {
        _persons = new ArrayList<Person>();
    }
    
    public static AddressBook getInstance()
    {
        if (_instance == null)
        {
            _instance = new AddressBook();
        }
        
        return _instance;
    }
    
    public void addPerson(Person p)
    {
        _persons.add(p);
    }
    
    public void deletePerson(long id)
    {
        Person p = getPerson(id);
        _persons.remove(p);
    }
    
    public Person getPerson(long id)
    {
        Person pers = null;
        
        for (Person p : _persons)
        {
            if (p.getID() == id)
                pers = p;
        }
        
        return pers;
    }
    
    public void updatePerson(long id, String name, String address, String zip, String city, String phone)
    {
        Person per = null;
        for (Person p : _persons)
        {
            if (p.getID() == id)
                per = p;
        }
        
        per.setName(name);
        per.setAddress(address);
        per.setZip(zip);
        per.setCity(city);
        per.setPhone(phone);
    }
    
    public ArrayList<Person> listAllPersons()
    {
        return _persons;
    }
}
